package com.ruoyi.web.controller.user;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.Passenger;
import com.ruoyi.system.domain.User;

import com.ruoyi.system.domain.vo.DriverInfo;
import com.ruoyi.system.service.DriverService;
import com.ruoyi.system.service.PassengerService;
import com.ruoyi.system.service.UserService;
import com.ruoyi.web.controller.dto.LoginUser;
import com.ruoyi.web.controller.dto.OrderTest;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;

@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private DriverService driverService;

    @Autowired
    private PassengerService passengerService;


    /**
     * 司机-乘客的登录方法
     * @param userName
     * @param userPwd
     * @return
     */
    @GetMapping("/userLogin")
    public AjaxResult userLogin(@RequestParam("userName") String userName, @RequestParam("userPwd") String userPwd) {
        //判断账号存不存在
        User userDB = userService.selectUserByUserName(userName);
        if (userDB==null){
            return AjaxResult.error("帐号不存在");
        }
        //判断密码是否则正确
        if (!userDB.getUserPwd().equals(userPwd)){
            return AjaxResult.error("账号或密码错误");
        }

        //修改登录时间和登录状态
        userDB.setLoginTime(new Date());
        userDB.setUserStatus(1L);

        Boolean res= userService.updateByUserDB(userDB);
        if (!res){
            return AjaxResult.error("上线失败");
        }
        HashMap<String, Object> map = new HashMap<>();
        if (userDB.getRoleId()==1){
            //判断是司机 则 去查司机的个人 信息
            DriverInfo driverInfo= driverService.getDriverInfo(userDB.getUserCode());
            String token = Jwts.builder().signWith(SignatureAlgorithm.HS256, "heruifeng")
                    .claim("id", userDB.getUserCode())
                    .compact();
            driverInfo.setToken(token);

            LoginUser loginUser=new LoginUser();
            loginUser.setUserName(userDB.getUserName());
            loginUser.setPassword(userDB.getUserPwd());
            loginUser.setToken(token);


            map.put("driverInfo", driverInfo);
            map.put("userInfo", loginUser);
            return AjaxResult.success("司机",map);

        }
        //乘客登录
        if (userDB.getRoleId()==2){
            String token = Jwts.builder().signWith(SignatureAlgorithm.HS256, "heruifeng")
                    .claim("id", userDB.getUserCode())
                    .compact();
            //查询乘客的个人信息
            Passenger passenger=passengerService.selectPassengerByUserCode(userDB.getUserCode());

            LoginUser loginUser=new LoginUser();
            loginUser.setUserName(userName);
            loginUser.setPassword(userPwd);
            loginUser.setToken(token);

            map.put("getUserInfo",loginUser);
            map.put("passenger",passenger);
            return AjaxResult.success("乘客",map);
        }

        return AjaxResult.error("查无此人");

    }









}
